<?php
class comment
{
	public $table;
	public $pages;
	public $db;
	public $config;
	public $current_user;
	
	function comment()
	{
		global $db,$current_user,$comment_config;
        $this->db = &$db;
        $this->table = DB_PRE.'comment';
        $this->current_user = $current_user;
        $this->config = $comment_config;
	}
	
	/**
	 * 编辑单条评论信息
	 *
	 * @param array $data
	 * @param int $g_id
	 * @return bool
	 */
	function edit_comment($data,$commentid)
	{
		$params = $data;
		$params['updatetime'] = time();
		$res = $this->db->simple_update($this->table,$params,array('commentid'=>$commentid));
		return $res;
	}
	
	/**
	 * 根据id获得评论信息
	 *
	 * @param int $commentid
	 * @param char $column
	 * @return array
	 */
	function get_comment_by_id($commentid,$column='*')
	{
		$this->db->prepare('select '.$column.' from '.$this->table.' where commentid=? and status=?');
		$this->db->set_string($commentid);
		$this->db->set_int(STATUS_NORMAL);
		$comment = $this->db->execute(true);
		return $comment;
	}
	
	/**
	 * 获得评论列表
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param int $flag
	 * @param int $nid
	 * @param int $passed
	 * @param varchar $column
	 */
	function get_comment($page,$pagesize,$flag,$nid,$passed,$kw,$time,$column='a.*')
	{
		if(!in_array($flag, $this->config))
		{
			$flag = array_shift(array_keys($this->config));
		}
		$start = ($page - 1) * $pagesize;
		$wheres = array(
			'a.status'	=>	array('set_int', STATUS_NORMAL, '='),
			'a.flag'		=>	array('set_int', $flag, '=')
		);
		if($nid !== false)
		{
			$wheres['a.nid'] = array('set_string', $nid, '=');
		}
		if($passed !== false)
		{
			$wheres['a.passed'] = array('set_int', $passed, '=');
		}
		if($kw !==false)
		{
			$wheres['a.content'] = array('set_string','%' . $kw . '%',' like ');
		}
		
		//时间
		if($time !== false && intval($time) >0)
		{
			$m=$d=$y=0;
			$dw = preg_replace('/[0-9]*/','',$time);
			$$dw = intval($time);
			if($dw == 'w')  //每周定义为7天
			{
				$d = $w * 7;
			}
			$start_time = mktime(0,0,0,(date('m')-$m),(date('d')-$d),(date('Y')-$y));
			$end_time = time();
			$wheres['a.addtime'] = array('set_int', $start_time, '>=');
		}
		$config = $this->config[$flag];
		$tmp_array = array();
		foreach ($wheres as $k=>$where)
		{
			$tmp_array[] = $k .$where[2].'?';
		}
		$where_columns = implode(' and ',$tmp_array);
		unset($tmp_array);
		$join_condition = 'a.nid='.$config['join'].' and '.implode(' and ',$config['condition']); //设置宿主查询条件
		$config_columns = array();
		foreach ($config['column'] as $k=>$v)
		{
			$config_columns[] = $v . ' as ' . $k;
		}
		$this->db->prepare('select count(*) as nums from '.$this->table.' as a join '.$config['table'].' on '.$join_condition . ' where ' .$where_columns);
		foreach ($wheres as $where)
		{
			$func = $where[0];
			$this->db->$func($where[1]);
		}
		$c_res = $this->db->execute(true);
		$this->db->prepare('select '.$column.','.implode(',',$config_columns).' from '.$this->table.' as a join '.$config['table'].' on '.$join_condition .' where ' . $where_columns . ' order by a.addtime desc limit ?,?');
		foreach ($wheres as $where)
		{
			$func = $where[0];
			$this->db->$func($where[1]);
		}
		$this->db->set_int($start);
		$this->db->set_int($pagesize);
		$res = $this->db->execute();
		$cout = $c_res['nums'];
		$this->pages = pages($cout,$page,$pagesize);
		return $res;
	}
}
?>